﻿#include "CSqlOperPub.h"
#include "cpublicdataex.h"
#include <QDateTime>

/*条件查找进出厂记录*/
//nTimeType      时间类型 0-入场时间 1-出厂时间
//sRecordTime	 起始时间
//sLeaveTime	 结束时间
//sCarCode		 车牌号
//sCargoCode	 货票号
//sViewCardCode  可视卡号
bool CSqlOperPub::selCdRlRecMstqyInfo(QVector<QSqlRecord>& vecRecord, int nTimeType, QString sStartTime, QString sEndTime, QString sCarCode, QString sCargoCode, QString sViewCardCode, QString & sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "RLRECORDMSTQY";
	cData.sSql = "SELECT A.RECORD_NO, A.TRANS_TYP, A.CAR_ID, A.TICK_NO, A.CARD_ID, A.MATERIAL_TYPE, A.CZ_DTM, A.JQ_DTM, A.ASH_TYPE, "
		"B.VEN_NAME, B.TOPRINT, A.RECORD_DTM, A.LEAVE_DTM, A.MZ_QTY, A.PZ_QTY, A.NET_QTY, A.ISPRINT, C.REMARK as CARRIER_NAME "
		"FROM RLRECORDMSTQY A "
		"LEFT JOIN VENDOR_INFO B ON A.VEN_NO = B.VEN_NO "
		"LEFT JOIN CARRIER_INFO C ON A.CARRIER_NO = C.CARRIER_NO "
		"WHERE TRANS_TYP IN ('2','3') AND "
		"(A.ASH_TYPE = '2' OR "
		"A.MATERIAL_TYPE in ('201','202','203','204','209','210','211','213','214','215') OR "
		"(A.CARRIER_NO IN (45, 201, 203, 204) AND A.ASH_TYPE IN ('1','2','3'))) ";

	//入厂时间
	if (0 == nTimeType)
	{
		cData.sSql.append("AND A.RECORD_DTM BETWEEN TO_DATE('" 
			+ sStartTime + "', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + sEndTime + "', 'YYYY-MM-DD HH24:MI:SS') ");
	}
	//出厂时间
	else if(1 == nTimeType)
	{
		cData.sSql.append("AND A.LEAVE_DTM BETWEEN TO_DATE('"
			+ sStartTime + "', 'YYYY-MM-DD HH24:MI:SS') AND TO_DATE('" + sEndTime + "', 'YYYY-MM-DD HH24:MI:SS') ");
	}

	//输入了车牌号
	if (!sCarCode.isEmpty())
	{
		cData.sSql.append("AND A.CAR_ID LIKE '%" + sCarCode + "%' ");
	}

	//输入了货票号
	if (!sCargoCode.isEmpty())
	{
		cData.sSql.append("AND A.TICK_NO LIKE '%" + sCargoCode + "%' ");
	}

	//输入了可视卡号
	if (!sViewCardCode.isEmpty())
	{
		cData.sSql.append("AND A.CARD_ID LIKE '%" + sViewCardCode + "%' ");
	}

	cData.sSql.append("ORDER BY LEAVE_DTM DESC");

	bool bRet = CPublicDataEx::instance().m_cDataBaseOperMgr.exec(cData);
	sErrMsg = cData.sErr;
	vecRecord = cData.vecRecord;

	return bRet;
}

//更新打印字段
bool CSqlOperPub::updatePrintState(QString sRecordId, QString & sErrMsg)
{
	CDatabaseExecData cData;
	cData.sTableName = "RLRECORDMSTQY";
	cData.sSql = "update RLRECORDMSTQY set ISPRINT=?"
		" where RECORD_NO=?";

	cData.addValue("ISPRINT", "Y");
	cData.addValue("RECORD_NO", sRecordId);

	bool bRet = CPublicDataEx::instance().m_cDataBaseOperMgr.exec(cData);
	sErrMsg = cData.sErr;
	return bRet;
}

CSqlOperPub::CSqlOperPub()
{

}

CSqlOperPub::~CSqlOperPub()
{

}
